package data;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.concurrent.ExecutionException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;

/**
 * This class is a class that allows you to do a simple Database task.
 * 
 * @author Pooja
 * 
 * @param <Params>
 * @param <Progress>
 * @param <Result>
 */
abstract public class DBTask<Params, Progress, Result> extends
		AsyncTask<Params, Progress, Result> {

	/** Connection to a MySQL database */
	protected Connection conn;

	/** Android dialog showing database connection progress */
	private ProgressDialog progress;

	/** Android activity to display progress on */
	protected Activity activity;

	/**
	 * Gets the result of the database operation
	 * 
	 * @return result of database operation
	 * @throws DBInterfaceError
	 */
	abstract public Result getResult() throws DBInterfaceError;

	/**
	 * This constructor takes in a Activity so that it can show a loading bar.
	 * 
	 * @param activity
	 *            - the activity necessary for the loading bar
	 * @throws DBInterfaceError
	 */
	public DBTask(final Activity activity) throws DBInterfaceError {
		this.activity = activity;

		// if (conn != null){
		// System.out.println("yo3");
		// } else {
		getConn();
		// }
	}

	/**
	 * creates a connection to a database and stores it in conn
	 * 
	 * @throws DBInterfaceError
	 */
	public void getConn() throws DBInterfaceError {
		AsyncTask<Void, Void, Connection> getConn = new AsyncTask<Void, Void, Connection>() {
			private ProgressDialog dialog;

			@Override
			protected void onPreExecute() {
				System.out.println("hi 52 5252 5");
				dialog = new ProgressDialog(activity);
				dialog.show();
			}

			@Override
			protected Connection doInBackground(Void... arg0) {

				try {
					Class.forName("com.mysql.jdbc.Driver").newInstance();
				} catch (InstantiationException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				}

				Connection conn = null;
				try {
					conn = DriverManager
							.getConnection("jdbc:mysql://instance39727.db.xeround.com:7503/wheresmystuff?user=admin&password=panda");
					System.out.println("yeahh!!!");
				} catch (SQLException e) {
					System.err.println("panndaaaa");
					e.printStackTrace();
				}

				return conn;
			}

			@Override
			protected void onPostExecute(Connection result) {
				if (dialog.isShowing()) {
					dialog.dismiss();
				}
			}

		};

		getConn.execute();

		try {
			conn = getConn.get();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}

		if (conn != null) {
			System.out.println("Connected to database");
		} else {
			throw new DBInterfaceError(DBInterfaceError.Error.CONNECTION_ERROR);
		}

		System.out.println("Connected to database");
	}

	public ProgressDialog getProgress() {
		return progress;
	}

	public void setProgress(ProgressDialog progress) {
		this.progress = progress;
	}

}